package com.recipedb.recipedb.util;

import java.lang.reflect.Field;

public class MergeUtils {

    /**
     * PUT 语义：incoming 的 null 字段补成 existing 的旧值
     */
    public static <T> T mergeForPut(T existing, T incoming) {
        if (existing == null || incoming == null) return incoming;

        Class<?> clazz = existing.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            try {
                Object newValue = field.get(incoming);
                if (newValue == null) {
                    field.set(incoming, field.get(existing));
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return incoming;
    }
}
